<?php
/*  
Submits and processes events for submission
*/

$title = "Add an Event";
include('includes/header.php');

/* Process post data */

if ($_POST) {
	//format start date
	$startdate = $_POST['syear'] . "-" . $_POST['smonth'] . "-" . $_POST['sday'];
	
	//format time
	$shour = $_POST['shour'];
	if ($_POST['sampm'] == 1) $shour = $shour + 12;
	$ehour = $_POST['ehour'];
	if ($_POST['eampm'] == 1) $ehour = $ehour + 12;
	
	//adjust time if all day event
	if ($_POST['allday']==0) {
		$stime = $shour . ":" . $_POST['smin'];
		$etime = $ehour . ":" . $_POST['emin'];
		$allday = 0;
		$eday = $_POST['eday'];
	} else {
		$allday = 1;
		$stime = "00:00:00";
		$etime = "00:00:00";
		$eday = $_POST['eday'] + 1;
	}
	
	//format end date
	$enddate = $_POST['eyear'] . "-" . $_POST['emonth'] . "-" . $eday;
	$user = get_user($_SESSION['UserID']);
	$apv = $user['approved'];

	//save the data
	if ($id=add_event($_POST['title'],$_SESSION['UserID'],$allday,$startdate,$stime,$enddate,$etime,$_POST['desc'],$apv)) {
		//save the meta-data
		foreach ($_POST['meta_type'] as $key => $meta_type) {
			if (strlen($_POST['meta_value'][$key])>0) {
				add_meta($id,$meta_type,$_POST['meta_value'][$key]);
			}
		}
	echo "<div class='good-news'>";
	if ($apv == 1) echo "Event Added.";
	else echo "Event Awaiting Moderation.";
	echo "</div>";
	}

	}
?>
<script>
	function toggle_time() {
		if (document.getElementById('allday[1]').checked) {
			document.getElementById('stime').style.visibility = 'hidden';
			document.getElementById('etime').style.visibility = 'hidden';
		} else {
			document.getElementById('stime').style.visibility = 'visible';
			document.getElementById('etime').style.visibility = 'visible';
		}
	}
</script>
<?php 	
$metas = get_meta_keys();
$i=0;
?>
<script>
	function addMetaElement() {
	  var ni = document.getElementById('MetaDiv');
	  var num = (document.getElementById('MetaStartValue').value -1)+ 1;
		document.getElementById('MetaStartValue').value = parseInt(document.getElementById('MetaStartValue').value) + 1;
	  var newdiv = document.createElement('div');
	  var divIdName = 'my'+num+'Div';
	  var contents;
	  newdiv.setAttribute('id',divIdName);
	  contents = "	<div class='form-row'>\n\
		<div class='form-label'>\n\
				<select name='meta_type["+num+"]'>\n\
					<?php foreach ($metas as $ID => $meta) { ?>\n\
						<option value='<?php echo $ID; ?>'";
						if (num+1 == '<?php echo $ID; ?>') {
						contents = contents + " SELECTED='SELECTED'";
						}
						contents = contents + "><?php echo $meta['desc']; ?></option>\n\
					<?php } ?>\n\
				</select>\n\
		</div>\n\
		<div class='form-field'><input type='text' name='meta_value["+num+"]' id='meta_value["+num+"]' size='50' /></div>\n\
	</div>";
	  newdiv.innerHTML = contents;
	  ni.appendChild(newdiv);
	}
</script>
<form name="addevent" id="addevent" method="post" >
	<div class="form-row">
		<div class="form-label">
			<label for="title">Title: </label>
		</div>
		<div class="form-field">
			<input type="text" name="title" id="title" value=""  size="50"/>
		</div>
	</div>
	<div class="form-row">
		<div class="form-label">
			<label for="allday">All Day Event: </label>
		</div>
		<div class="form-field">
			<input type="radio" name="allday" id="allday[0]" checked="true" onChange='toggle_time()'/><label for="allday[0]">No</label><br />
			<input type="radio" name="allday" id="allday[1]" onChange='toggle_time()'/><label for="allday[1]">Yes</label><br />
		</div>
	</div>
	<div class="form-row">
		<div class="form-label">
			<label for="sday">Start Date: </label>
		</div>
		<div class="form-field">
			<select name="smonth" id="smonth" onchange="document.getElementById('emonth').value=this.value" >
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="sday" id="sday" onchange="document.getElementById('eday').value=this.value" >
			<?php for ($i = 1; $i <= 31; $i++) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="syear" id="syear" onchange="document.getElementById('eyear').value=this.value">
			<?php for ($i = date('Y'); $i <= date('Y') + 1; $i++) { ?>
				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
		</div>
	</div>
	<div class='form-row' id='stime'>
		<div class='form-label'>
			<label>Start Time:</label>
		</div>
		<div class='form-field'>
			<select name="shour" id="shour" onchange="document.getElementById('ehour').value=this.value" >
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 : 
				<select name="smin" id="smin" onchange="document.getElementById('emin').value=this.value" >
			<?php for ($i = 0; $i < 60; $i=$i+15) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo make_two($i); ?></option>
			<?php } ?>
			</select>
			 &nbsp; 
			<select name="sampm" id="sampm" onchange="document.getElementById('eampm').value=this.value" >
				<option value='0'>AM</option>
				<option value='1'>PM</option>
			</select>
		</div>
	</div>
		<div class="form-row">
		<div class="form-label">
			<label for="eday">End Date: </label>
		</div>
		<div class="form-field">
			<select name="emonth" id="emonth" >
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="eday" id="eday">
			<?php for ($i = 1; $i <= 31; $i++) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="eyear" id="eyear">
			<?php for ($i = date('Y'); $i <= date('Y') + 1; $i++) { ?>
				<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
		</div>
	</div>
	<div class='form-row' id='etime'>
		<div class='form-label' >
			<label>End Time:</label>
		</div>
		<div class='form-field'>
			<select name="ehour" id="ehour">
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 : 
				<select name="emin" id="emin" >
			<?php for ($i = 0; $i < 60; $i=$i+15) { ?>
				<option value="<?php echo make_two($i); ?>"><?php echo make_two($i); ?></option>
			<?php } ?>
			</select>
			 &nbsp; 
			<select name="eampm" id="eampm">
				<option value='0'>AM</option>
				<option value='1'>PM</option>
			</select>
		</div>
	</div>
	<div class='form-row'>
		<div class='form-label'>
			<label for='desc'>Description:</label>
		</div>
		<div class='form-field'>
			<textarea name='desc' id='desc' rows='7' cols='38'></textarea>
		</div>
	</div>
<?php  
	$metas = get_meta_keys();
	$i=0;
	$num_fields = 1; //Number of initial fields to display, feel free to change this
	$keys = array_keys($metas);
	while ($i < sizeof($metas) && $i<$num_fields) { ?> 
	<div class='form-row'>
		<div class='form-label'>
				<select name='meta_type[<?php echo $i; ?>]'>
					<?php foreach ($metas as $ID => $meta) { ?>
						<option value='<?php echo $ID; ?>'<?php if ($ID == $keys[$i]) echo " SELECTED";?>><?php echo $meta['desc']; ?></option>
					<?php } ?>
				</select>
		</div>
		<div class='form-field'><input type='text' name='meta_value[<?php echo $i; ?>]' id='meta_value[<?php echo $i; ?>]' size='50' /></div>
	</div>
	<?php $i++; } ?>
	<input type="hidden" value="<?php echo $i; ?>" id="MetaStartValue" />
	<div id="MetaDiv"> </div>
	<div class='form-row'>
		<div class='form-label'>
		 &nbsp;
		</div>
		<div class='form-field'>
			<input type='submit' value='Add Event'> [ <a href="javascript:;" onclick="addMetaElement();">Add Another Field</a> ]
		</div>
	</div>
</form>
<div class='clear'> </div>
<?php 
include('includes/footer.php');
?>
	